package cloudUser.controller;

import cloudEntity.dto.user.SmartUserDTO;
import cloudEntity.entity.SmartUserEntity;
import cloudEntity.utils.ResultUtils;
import cloudUser.service.SmartUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: Smart School
 * @ClassName authController
 * @description: 授权控制器
 * @author: 陈智博
 * @create: 2025-03-27 11:37
 * @Version 1.0
 **/
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private SmartUserService smartUserService;

    @RequestMapping(value = "/flogin",method = RequestMethod.POST)
    public ResultUtils AuthLogin(@RequestBody SmartUserDTO smartUserDTO,
                                 HttpServletRequest httpServletRequest)
    {
        return ResultUtils.success("数据返回成功!",smartUserService.login(smartUserDTO,httpServletRequest));
    }

    @RequestMapping(value = "/getUid",method = RequestMethod.POST)
    public SmartUserEntity smartUserEntity()
    {
        return (SmartUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    @RequestMapping(value = "/permission",method = RequestMethod.POST)
    public List getUserPermissionList()
    {
        List<String> permissionList = new ArrayList<>();
        for (GrantedAuthority grantedAuthority : SecurityContextHolder.getContext().getAuthentication().getAuthorities().stream().toList()) {
            permissionList.add(grantedAuthority.getAuthority());
        }
        return permissionList;
    }
}
